



var session=request.getSession();
if(typeof session==='undefined'){

	
	response.redirect("/login",request,response);
	response.end();

}else if(session.LoggedIn){


	request.user={
			username:session.user["username"],
			name:session.user["name"],
			picture:session.user["picture"]
				
		};
	//get search results from DB
		
	var searchStr=request.parameters["txtSearch"];



	var boardController=request.require("./boardController");
	boardController.querySearch(request.user.username,searchStr,request,response,cb);

	

	

	
}else{
	
	response.redirect("/login",request,response);
	response.end();

}


function cb(boards,queryStr,request,response){

	
	
	var finalBoards={};
	for(var i=0;i<boards.length;i++){
	
		if(finalBoards[boards[i].name]==undefined){
			finalBoards[boards[i].name]=boards[i];
		}
		else if(finalBoards[boards[i].name].memberName!=request.user.username){
		
			if(boards[i].memberName==request.user.username){
				finalBoards[boards[i].name]=boards[i];
			}
		
		}
		
	}

	var newarray = [];
	for (x in finalBoards){
     newarray.push(finalBoards[x]);
	}

	request.setAttribute("boards",newarray);
	request.searchQuery=queryStr;

	response.forward("/search/search.jst",request,response);
	response.end();

	
}
